package com.devops.admin.controller;

import com.devops.admin.po.DOSChartImportPo;
import com.devops.admin.service.DOSHelmChartService;
import com.devops.admin.service.DOSLogsService;
import com.devops.admin.service.DOSOracleAwrReportService;
import com.devops.common.annotation.DOSLog;
import com.devops.common.utils.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author heyabo
 */
@RestController
@Api(tags = "文件上传相关",value = "/fileAuthority")
@RequestMapping("/fileAuthority")
@Slf4j
public class FileController {

    @Autowired
    DOSHelmChartService dosHelmChartService;
    @Autowired
    DOSLogsService dosLogsService;
    @Autowired
    DOSOracleAwrReportService dosOracleAwrReportService;

    @PostMapping("/import-helm")
    @ApiOperation(value = "chart上传", notes = "先上传到服务器，再次推送到阿里云helm-chart仓库")
    @DOSLog(description = "chart上传")
    public Message importHelm(@RequestParam(value = "chartVo") String chart,
                                      @RequestParam(value="chart") MultipartFile file){
        try{
            DOSChartImportPo po = dosHelmChartService.importHelm(file,chart);
            if (null!=po) {
                return Message.ok(po);
            }else {
                return Message.error("上传失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Message.error(e.getMessage());
        }
    }

    @PostMapping("/awr/report")
    @ApiOperation(value = "awr报告上传", notes = "上传awr报告到oss")
    public Message uploadAwrReport(@RequestParam(value="file") MultipartFile file){
        try{
            if (null != file) {
                return Message.ok(dosOracleAwrReportService.uploadOSS(file));
            }else {
                return Message.error("文件不能为空");
            }
        }catch (Exception e){
            return Message.error(e.getMessage());
        }
    }

    @PostMapping("/v2/awr/report")
    @ApiOperation(value = "awr报告上传v2", notes = "上传awr报告到oss")
    public Message uploadAwrReportV2(@RequestParam(value="file") MultipartFile file){
        try{
            if (null != file) {
                return Message.ok(dosOracleAwrReportService.uploadOSSV2(file));
            }else {
                return Message.error("文件不能为空");
            }
        }catch (Exception e){
            return Message.error(e.getMessage());
        }
    }
}
